<?php

class task extends bhCommon {


    public function index()
    {
        $sql = "SELECT * FROM " . prefix('task') . " WHERE `position` = 1" . get_uniacid();
        $task = p_fetchall($sql);
        $new_task = array();
        if ($task) {
            $same_day = strtotime(date('Y-m-d'));
            foreach ($task as & $line) {
                $w = array(
                    'member_id' => $this->member['id'],
                    'task_id' => $line['id'],
                    'same_day' => $same_day
                );
                $m = p_get('task_member', $w);
                $line['state'] = 1;
                $line['is_succ'] = 1;
                $line['succ_num'] = 0;

                $line['num'] = 0;

                if (!empty($m)) {

                    $line['num'] = $m['number'];

                    if (in_array($line['type'], array(2))) {
                        $line['state'] = 3;
                        $line['succ_num'] = 1;
                    } else {
                        $line['succ_num'] = $m['number'];
                        $line['state'] = $m['number'] >= $line['number'] ? 3 : 1;
                    }
                } else {
                }

                if ($line['state'] != 3) {
                    $line['currency'] = floatval($line['currency']);
                    $new_task[] = $line;
                    if (count($new_task) >= 4) {
                        break;
                    }
                }
            }
        }
        $data = array(
            'task' => $new_task,
            'member' => $this->member
        );
        $this->json($data);
    }






    /**
     * 领取
     */
    public function receive()
    {
        $id = $this->get('id');
        $task = p_get('task', array('id' => $id));

        if (empty($task)) {
            $this->json('任务不存在', 2);
        }
        $same_day = strtotime(date('Y-m-d'));


        $task_member = p_get('task_member', array('member_id' => $this->member['id'], 'task_id' => $task['id'], 'same_day' => $same_day));

        if ($task_member && (in_array($task['type'], array(3)) || ($task['type'] == 2 && $task_member['number'] >= $task['number']))) {
            $this->json();
        }
        pdo_begin();
        try{
            if (!$this->rising->change_currency($this->member['id'], $task['currency'], 7, $task['title'])) {
                throw new Exception($this->rising->error);
            }
            if ($task_member && $task['type'] == 2) {
                if (!p_update('task_member', array('number' => $task_member['number'] + 1), array('id' => $task_member['id']))) {
                    throw new Exception('奖励领取失败~');
                }
            } else {
                $insert = array(
                    'member_id' => $this->member['id'],
                    'task_id' => $id,
                    'same_day' => $same_day,
                    'created' => time()
                );
                if (!p_insert('task_member', $insert)) {
                    throw new Exception('奖励领取失败~');
                }
            }
            pdo_commit();
            $this->json();
        } catch (Exception $e) {
            pdo_rollback();
            $this->json($e->getMessage(), 2);
        }
    }

}